package Blog;

import org.junit.jupiter.api.*;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.CsvFileSource;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.provider.ValueSource;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;

import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;

import static java.lang.Thread.sleep;
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class BlogCases extends InitAndEnd{
    public static Stream<Arguments> Generator() {
        return Stream.of(
                Arguments.arguments("http://localhost:8080/blog_sysytem/blog_detail.html?blogId=21",
                        "文章详情页","我是张三2号")
        );
    }

    /**
     * 输入正确的账号,密码登录成功
     */
    @Order(1)
    @ParameterizedTest
    @CsvFileSource(resources = "loginSuccess.csv")
    void loginSuccess(String username,String password) throws InterruptedException {
        // 打开博客登录页面
        webDriver.get("http://localhost:8080/blog_sysytem/login.html");
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        // 输入admin
        webDriver.findElement(By.cssSelector("#username")).sendKeys(username);
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);

        // 输入密码123456
        webDriver.findElement(By.cssSelector("#password")).sendKeys(password);
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);

        // 点击登录成功
        webDriver.findElement(By.cssSelector("#submit")).click();
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);

//        sleep(3000);
//        webDriver.switchTo().alert().accept();

        // 跳转到列表页
        // 获取当前页面url
        String cur_url = webDriver.getCurrentUrl();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        // http://localhost:8080/myblog_list.html 测试通过
        Assertions.assertEquals("http://localhost:8080/blog_sysytem/blog_list.html",cur_url);

        String cur_admin = webDriver.findElement(By.cssSelector("body > div.container > div.container-left > div > h3")).getText();
        Assertions.assertEquals(username,cur_admin);
    }

    @Test
    @Order(2)
    void BlogList() {
        // 打开博客列表页面
        webDriver.get("http://localhost:8080/blog_sysytem/blog_list.html");
        // 获取页面上所有博客标题对应的元素
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        int size = webDriver.findElements(By.cssSelector(".title")).size();
        // 如果元素数量不为0 测试通过
        Assertions.assertNotEquals(0,size);
    }

    /**
     * 博客详情页的校验
     * url
     * 标题
     * 页面的title是博客详情页
     */
    @Order(3)
    @ParameterizedTest
    @MethodSource("Generator")
    void BlogDetail(String expected_url,String expected_title,String expected_blogTitle) {
        // 找到第一篇博客对应的全文查看的按钮
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        webDriver.findElement(By.cssSelector("body > div.container > div.container-right > div:nth-child(1) > a")).click();
        // 获取当前页面的url
        String cur_url = webDriver.getCurrentUrl();
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        // 获取当前也免得title
        String cur_title = webDriver.getTitle();
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        // 获取博客详情页的博客标题
        String blog_title = webDriver.findElement(By.cssSelector("body > div.container > div.container-right > h3")).getText();
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
//        Assertions.assertEquals(expected_url,cur_url);
        Assertions.assertEquals(expected_title,cur_title);
        //Assertions.assertEquals(expected_blogTitle,blog_title);
    }

    /**
     * 写博客
     */
    @Order(4)
    @Test
    void EditBlog() throws InterruptedException {
        // 找到写博客按钮,点击
        webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(5)")).click();
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        // 找到输入框,输入标题(通过JS将内容输入)
        ((JavascriptExecutor)webDriver).executeScript("document.getElementById(\"title\").value=\"自动化测试12\"");
        sleep(3000);
        // 点击发布
        webDriver.findElement(By.cssSelector("#submit")).click();
        sleep(3000);
        // 获取当前url
        String cur_url = webDriver.getCurrentUrl();
        Assertions.assertEquals("http://localhost:8080/blog_sysytem/blog_list.html",cur_url);
    }

    /**
     * 校验已发布博客标题
     * 校验已发布博客时间
     */

    @Order(5)
    @Test
    void BlogInfoChecked() {
        webDriver.get("http://localhost:8080/blog_sysytem/blog_list.html");
        // 获取最新一篇博客标题
        String first_title = webDriver.findElement(By.cssSelector("body > div.container > div.container-right > div:nth-child(1) > div.title")).getText();
        // 获取第一篇博客的发布时间
        String first_time = webDriver.findElement(By.xpath("/html/body/div[2]/div[2]/div[1]/div[2]")).getText();
        // 校验博客标题
        Assertions.assertEquals("自动化测试12",first_title);
        // 如果时间是2023年发布的,测试通过
        if (first_time.contains("2023")) {
            System.out.println("测试通过");
        }else {
            System.out.println("当前时间: " + first_time);
            System.out.println("测试不通过");
        }
    }

    /**
     * 删除发布的博客
     */
    @Order(6)
    @Test
    void DeleteBlog() {
        // 打开我的主页
        webDriver.get("http://localhost:8080/blog_sysytem/blog_list.html");
    }
}
